package com.laf.manage.controller.admin;

import com.laf.common.annotation.Log;
import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.AjaxResult;
import com.laf.common.core.page.TableDataInfo;
import com.laf.common.core.text.Convert;
import com.laf.common.enums.BusinessType;
import com.laf.common.utils.ServletUtils;
import com.laf.common.utils.poi.ExcelUtil;
import com.laf.common.validation.ValidGroup;
import com.laf.manage.domain.dto.LostItemDTO;
import com.laf.manage.domain.dto.ReceiveDTO;
import com.laf.manage.domain.dto.RollbackDTO;
import com.laf.manage.domain.po.LostItem;
import com.laf.manage.enums.QrTypeEnum;
import com.laf.manage.service.ILostItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

import static com.laf.common.core.page.TableSupport.PAGE_NUM;
import static com.laf.common.core.page.TableSupport.PAGE_SIZE;

/**
 * 失物管理Controller
 *
 * @author zian
 * @date 2024-11-01
 */
@Slf4j
@Api(tags = "失物管理管理")
@RestController("adminLostItemController")
@RequestMapping("/admin/lostItem")
public class LostItemController extends BaseController {
    @Autowired
    private ILostItemService lostItemService;

    /**
     * 驳回物品：将物品状态改为驳回
     */
    @Log(title = "失物管理", businessType = BusinessType.UPDATE)
    @PutMapping("/reject")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:edit')")
    @ApiOperation("驳回物品")
    public AjaxResult reject(@RequestBody RollbackDTO dto) {
        log.info("驳回物品 参数有：{}", dto);
        lostItemService.rollback(dto , true);
        return AjaxResult.success();
    }

    /**
     * 通过审核，将物品状态从审核状态转换为待提交
     */
    @Log(title = "失物管理", businessType = BusinessType.UPDATE)
    @PutMapping("/passAudit/{id}")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:edit')")
    @ApiOperation("通过审核，将物品状态从审核状态转换为待提交")
    public AjaxResult passAudit(@PathVariable Long id) {
        boolean b = lostItemService.passAudit(id);
        return toAjax(b);
    }




    @GetMapping("/check/submitCode")
    @ApiOperation("校验提交码")
    @PreAuthorize("isAuthenticated()") // 只要通过认证都可以访问
    public AjaxResult checkSubmitCode(String uuid) {
        log.info("提交二维码是否有效 参数有：{}", uuid);
        lostItemService.checkCode(uuid, QrTypeEnum.SUBMIT_CODE.getKeyPrefix());
        return AjaxResult.success(true);
    }

    /**
     * 校验领取码
     *
     * @param uuid
     * @return
     */
    @GetMapping("/check/pickCode")
    @ApiOperation("校验领取码是否有效")
    public AjaxResult checkPickCode(String uuid) {
        log.info("校验领取码是否有效 参数有：{}", uuid);
        lostItemService.checkCode(uuid, QrTypeEnum.PICKED_CODE.getKeyPrefix());
        return success(true);
    }


    /**
     * 提交通过
     */
    @PutMapping("/submitPass/{id}")
    @ApiOperation("提交通过")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:edit')")
    public AjaxResult submitPass(@PathVariable Long id) {
        log.info("提交通过 参数有：{}", id);
        boolean b = lostItemService.submitPass(id);
        return toAjax(b);
    }

    /**
     * 领取物品
     */
    @PutMapping("/receive")
    @ApiOperation("领取物品")
    @Log(title = "失物管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('manage:lostItem:edit')")
    public AjaxResult receiveItem(@RequestBody ReceiveDTO dto) {
        log.info("领取物品 参数有：{}", dto);
        lostItemService.receiveItem(dto);
        return AjaxResult.success();

    }

    /**
     * 查询失物管理列表 es
     */
    @ApiOperation("查询失物管理列表")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(LostItem lostItem) {
        log.info("查询失物管理列表 参数有：{}", lostItem);
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        TableDataInfo result = lostItemService.selectLostItemList(lostItem, pageNum, pageSize);
        return result;
    }

    /**
     * 导出失物管理列表
     */
    @ApiOperation("导出失物管理列表")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:export')")
    @Log(title = "失物管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LostItem lostItem) {
        List<LostItem> list = lostItemService.selectLostItemList(lostItem);
        ExcelUtil<LostItem> util = new ExcelUtil<LostItem>(LostItem.class);
        util.exportExcel(response, list, "失物管理数据");
    }

    /**
     * 获取失物管理详细信息
     */
    @ApiOperation("获取失物管理详细信息")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(lostItemService.getById(id));
    }

    /**
     * 新增失物管理
     */
    @ApiOperation("新增失物管理")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:add')")
    @Log(title = "失物管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Crud.Create.class) LostItemDTO dto) {
        log.info("新增失物管理 参数有：{}", dto);
        boolean b = lostItemService.addLostItem(dto, true);
        return toAjax(true);
    }

    /**
     * 修改失物管理
     */
    @ApiOperation("修改失物管理")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:edit')")
    @Log(title = "失物管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editLostItem(@RequestBody @Validated(ValidGroup.Crud.Update.class) LostItemDTO dto) {
        log.info("修改失物管理 参数有：{}", dto);
        return toAjax(lostItemService.editLostItem(dto , true));
    }

    /**
     * 删除失物管理
     */
    @ApiOperation("删除失物管理")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:remove')")
    @Log(title = "失物管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        lostItemService.deleteByIds(Arrays.asList(ids));
        return success();
    }


    @GetMapping("/details/{id}")
    @ApiOperation("失物详情")
    @PreAuthorize("@ss.hasPermi('manage:lostItem:query')")
    public AjaxResult getDetailsById(@PathVariable Long id) {
        return success(lostItemService.getDetailsById(id , true ));
    }

}
